package com.guangzhi.weijianzhi.view;

import com.guangzhi.weijianzhi.R;

import android.app.Dialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;


public class LodingFragmentAllDialog extends DialogFragment {
	
	
	
	
	
	String content;
	static FragmentManager manager;
	
   static Handler handle = new Handler(){
	public void handleMessage(android.os.Message msg) {
		
		dialog.dismiss();
	};
};
	
	

private static LodingFragmentAllDialog dialog;	/**
	 * @param manager
	 * @param content 文字内容
	 */
	public static LodingFragmentAllDialog showLodingDialog(FragmentManager manager,String content){
		Fragment fragment = manager.findFragmentByTag("loading");
		FragmentTransaction ft = manager.beginTransaction();
		if(fragment != null){
			ft.remove(fragment);
		}
		dialog = LodingFragmentAllDialog.newInstance(content);
		ft.add(dialog,"loading");
		ft.commitAllowingStateLoss();
		
		 Message msg = new Message();
		
		handle.sendMessageDelayed(msg, 3000);
		return dialog;
	}
	
	
 
	
	
	/**
	 * 默认的文字提示
	 * @param manager
	 * @param resources
	 */
	public static LodingFragmentAllDialog showLodingDialog(FragmentManager manager,Resources resources){
	//	String content = resources.getString(R.string.loading);
		return showLodingDialog(manager, "马上来了");
	}
	
	public static void dismiss(FragmentManager fragmentManager){
		if(fragmentManager == null)return;
		DialogFragment fragment = (DialogFragment)fragmentManager.findFragmentByTag("loading");
		if(fragment != null)fragment.dismissAllowingStateLoss();
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = new Dialog(getActivity(),R.style.dialog);
		dialog.setCancelable(false);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setContentView(R.layout.loading_dialog2_layout);
		TextView textView = (TextView)dialog.findViewById(R.id.loadingTxt);
		textView.setText(content);
		return dialog;
	}
	
	public boolean isShowing(){
		return getDialog().isShowing();
	}
	
	static LodingFragmentAllDialog newInstance(String content){
		LodingFragmentAllDialog dialog = new LodingFragmentAllDialog();
		Bundle bundle = new Bundle();
		bundle.putString("content",content);
		dialog.setArguments(bundle);
		return dialog;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		content = getArguments().getString("content");
	}
}
